package com.jingdianjichi.circle.server.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.jingdianjichi.auth.entity.Result;
import com.jingdianjichi.circle.api.req.GetShareMessageReq;
import com.jingdianjichi.circle.api.vo.ShareMessageVO;
import com.jingdianjichi.circle.server.entity.po.ShareMessage;
import com.jingdianjichi.circle.server.service.ShareMessageService;
import com.jingdianjichi.subject.common.entity.PageResult;
import jdk.nashorn.internal.runtime.logging.Logger;
import lombok.extern.log4j.Log4j2;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 消息表(ShareMessage)表控制层
 *
 * @author makejava
 * @since 2024-06-07 15:51:32
 */
@RestController
@RequestMapping("/share/message")
@Log4j2
public class ShareMessageController {
    @Resource
    private ShareMessageService shareMessageService;

    @PostMapping("/getMessages")
    public Result<PageResult<ShareMessageVO>> getMessages(@RequestBody GetShareMessageReq req) {
        try {
            if (log.isInfoEnabled()) {
                log.info("消息入参:{}", JSON.toJSONString(req));
            }
            Preconditions.checkArgument(Objects.nonNull(req), "入参不能为空");
            PageResult<ShareMessageVO> messages = shareMessageService.getMessages(req);
            if (log.isInfoEnabled()) {
                log.info("消息出参:{}", JSON.toJSONString(messages));
            }
            return Result.ok(messages);
        } catch (IllegalArgumentException e) {
            log.error("消息参数异常！错误原因:{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        } catch (Exception e) {
            log.error("消息异常！错误原因:{}", e.getMessage(), e);
            return Result.fail("消息异常！");
        }
    }
}

